﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using THSLib;using THSERP.BaseProvider;
using DevExpress.XtraExport;

namespace THSERP.Modules.Report
{
    public partial class RP012 : ReportForm
    {
        public RP012()
        {
            InitializeComponent();
        }

        private void fld_btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RP012_Load(object sender, EventArgs e)
        {
        //    THSProgressBar.Start("Loading item list");
            fld_dgcICRP012InventoryStocks.Screen = this;
            fld_dgcICRP012InventoryStocks.InitializeControl();
            fld_lkeProductGroups.InitializeControl();
            InitializeDataSource();
          //  THSProgressBar.Close();
        }
        public void InitializeDataSource()
        {
            THSList<ICInventoryStocksInfo> lstICInventoryStock = new THSList<ICInventoryStocksInfo>();
            ICProductsController objProductsController = new ICProductsController();
            DataSet ds = objProductsController.GetAllObjects();
            if (ds.Tables.Count > 0)
            {

                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    ICProductsInfo objProductsInfo = (ICProductsInfo)objProductsController.GetObjectFromDataRow(row);
                    if (objProductsInfo != null)
                    {
                        ICInventoryStocksInfo objICInventoryStocksInfo = (ICInventoryStocksInfo)new ICInventoryStocksController().GetInventoryStockByProductAndStock(objProductsInfo.ICProductID, objProductsInfo.FK_ICStockID);
                        if (objICInventoryStocksInfo != null)
                        {
                            lstICInventoryStock.Add(objICInventoryStocksInfo);
                        }

                    }
                }
                fld_dgcICRP012InventoryStocks.DataSource = lstICInventoryStock;
                fld_dgcICRP012InventoryStocks.RefreshDataSource();
            }
        }

        private void fld_btnExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlgSave = new SaveFileDialog();
            dlgSave.Filter = "Microsoft Excel|*.xls";
            DialogResult dlgResult = dlgSave.ShowDialog();
            if (dlgResult == DialogResult.OK)
            {
                Cursor currentCursor = Cursor.Current;
                Cursor.Current = Cursors.WaitCursor;
                // this.fld_dgcARRP002InvoiceItems.ExportToXls(dlgSave.FileName);
                this.FindForm().Refresh();
                ExportXlsProvider provider = new ExportXlsProvider(dlgSave.FileName);
                DevExpress.XtraGrid.Export.BaseExportLink link = this.fld_dgcICRP012InventoryStocks.Views[0].CreateExportLink(provider);
                (link as DevExpress.XtraGrid.Export.GridViewExportLink).ExpandAll = false;
                link.ExportTo(true);
                provider.Dispose();
                Cursor.Current = currentCursor;
                if (DialogResult.Yes == MessageBox.Show("Xuất file Excel thành công! Bạn có muốn mở file ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    System.Diagnostics.Process.Start(dlgSave.FileName);
                }
            }
        }

        private void fld_btnPrint_Click(object sender, EventArgs e)
        {
            DevExpress.XtraPrinting.PrintingSystem printingSystem = new DevExpress.XtraPrinting.PrintingSystem();
            DevExpress.XtraPrinting.PrintableComponentLink printLink = new DevExpress.XtraPrinting.PrintableComponentLink();

            try
            {
                //Set to landscape
                printingSystem.PageSettings.Landscape = true;
                this.Cursor = Cursors.WaitCursor;
                printLink.Component = this.fld_dgcICRP012InventoryStocks;
                printLink.CreateDocument(printingSystem);
                printLink.VerticalContentSplitting = DevExpress.XtraPrinting.VerticalContentSplitting.Exact;
                printingSystem.PreviewFormEx.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                this.Cursor = Cursors.Default;
                printingSystem.Dispose();
                printLink.Dispose();
            }

        }

        private void fld_lkeProductGroups_EditValueChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(fld_lkeProductGroups.EditValue);
            List<ICInventoryStocksInfo> lstICInventoryStock =new List<ICInventoryStocksInfo>();
            ICProductsController objICProductsController =new ICProductsController();
            DataSet ds = objICProductsController.GetAllDataByForeignColumn("FK_ICProductGroupID", id);
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    ICProductsInfo objProductsInfo = (ICProductsInfo)objICProductsController.GetObjectFromDataRow(row);
                    if (objProductsInfo != null)
                    {
                        ICInventoryStocksInfo objICInventoryStocksInfo = (ICInventoryStocksInfo)new ICInventoryStocksController().GetInventoryStockByProductAndStock(objProductsInfo.ICProductID, objProductsInfo.FK_ICStockID);
                        if (objICInventoryStocksInfo != null)
                        {
                            lstICInventoryStock.Add(objICInventoryStocksInfo);
                        }

                    }
                }
                fld_dgcICRP012InventoryStocks.DataSource = lstICInventoryStock;
                fld_dgcICRP012InventoryStocks.RefreshDataSource();
            }
        
        //public void InitializeDataSource()
        //{
        //    THSList<ICProductAttributesInfo> lstItems = new THSList<ICProductAttributesInfo>();
        //    ICProductsController objProductsController = new ICProductsController();
        //    DataSet ds = objProductsController.GetAllObjects();
        //    if (ds.Tables.Count > 0)
        //    {
        //        ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
        //        foreach (DataRow row in ds.Tables[0].Rows)
        //        {
        //            ICProductsInfo objProductsInfo = (ICProductsInfo)objProductsController.GetObjectFromDataRow(row);
        //            DataSet ds1 = objProductAttributesController.GetLastAttributesByProductID(objProductsInfo.ICProductID);
        //            ProductAttributeList lstLastAttributes = new ProductAttributeList();
        //            lstLastAttributes.InitTHSList(null, THSUtil.GetTableNameFromBusinessObjectType(typeof(ICProductsInfo)), THSUtil.GetTableNameFromBusinessObjectType(typeof(ICProductAttributesInfo)));
        //            lstLastAttributes.Invalidate(ds1);
        //            lstLastAttributes.CalculateInventory(THSApp.CurrentCompanyInfo.FK_BRBranchID);
        //            foreach (THSTreeListObject obj in lstLastAttributes)
        //            {
        //                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
        //                objProductAttributesInfo.ICProductAttributeValue = objProductAttributesController.GetAttributeValueByProductAttributeID(objProductAttributesInfo.ICProductAttributeID);
        //                objProductAttributesInfo.ICProductDesc = objProductsInfo.ICProductDesc;
        //                objProductAttributesInfo.ICProductUnitCost = objProductsInfo.ICProductSupplierPrice;
        //                objProductAttributesInfo.ICProductUnitPrice = objProductsInfo.ICProductPrice01;
        //                objProductAttributesInfo.ICInventoryStockExtCost = objProductAttributesInfo.ICInventoryStockQuantity * objProductAttributesInfo.ICProductUnitCost;
        //                objProductAttributesInfo.ICInventoryStockExtPrice = objProductAttributesInfo.ICInventoryStockQuantity * objProductAttributesInfo.ICProductUnitPrice;
        //                lstItems.Add(objProductAttributesInfo);
        //            }
        //        }
        //    }
        //    fld_dgcICRP012Products.DataSource = lstItems;
        //    fld_dgcICRP012Products.RefreshDataSource();
        //}
    }
}